package cn.com.dhcc.app.pub.core.listener;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContextEvent;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.boot.BootParam;
import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.listener.AppInitListener;
import cn.com.dhcc.app.core.util.IpUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.app.pub.core.consts.TlqConfig;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.dao.registry.NodeInfoDao;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;

/**
 * 验证节点各项信息，ip，tlq，网络类型，节点编码
 * @日期：2016-1-5下午3:37:17
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
public class CheckNodeListener  implements AppInitListener {

	@Override
	public void init(ServletContextEvent contextEvent, BootParam bootParam)
			throws InitException {
		NodeInfoService nodeService = CM.getBean(NodeInfoService.class);
		NodeInfoDao nodeInfoDao = CM.getBean(NodeInfoDao.class);
		try {
			File uploadFolder = new File(AppConst.LOG_PATH);
			if (!uploadFolder.exists()) {
				uploadFolder.mkdirs();
			}
			File file = new File(AppConst.LOG_PATH,"start.log");
			if (!file.exists()) {
				file.createNewFile();
			}
			PrintStream printStream  = new PrintStream(new FileOutputStream(file, false));
			printStream.println("参数启动检查日期:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			//打印本机配置文件信息
			printStream.append("########################本机配置文件信息#####################");
			printStream.println();
			printStream.append("节点行政区编码： 【"+ NodeInfoParam.NODE_REGION_CODE.trim()+"】");
			printStream.println();
			printStream.append("网络类型： 【"+AppTagEl.ctname("NET_TYPE", NodeInfoParam.NODE_NET_TYPE)+"】");
			printStream.println();
			printStream.append("qcu_turbo：【"+TlqConfig.TLQ_BROKER_IP.trim()+":"+TlqConfig.TLQ_JMS_PORT+"】");
			printStream.println();
			printStream.append("qcu_turbo接收进程监听端口：【"+TlqConfig.TLQ_QUEUE_CONN_LISTEN_PORT+"】");
			printStream.println();
			printStream.append("本机IP：【"+AppConst.installIp+"】");
			printStream.println();
			printStream.append("本机映射IP：【"+AppConst.mappingIp+"】");
			printStream.println();
			printStream.append("本机是否是集群：【"+AppConst.isCluster+"】");
			printStream.println();
			printStream.append("#########################################################");
			printStream.println();
			NodeInfo node = nodeInfoDao.getLocalNodeNoNetType();
			
			if(node != null){
				//验证节点编码
				String nodeRegionCode = NodeInfoParam.NODE_REGION_CODE.trim();
				String localNodeCode = node.getNodeCode().substring(0, 6);
				if(!nodeRegionCode.equals(localNodeCode)){
					printStream.append("节点编码不一至   node.region.code["+nodeRegionCode+"]  已存在节点编码["+localNodeCode+"] 请检查");
					printStream.println();
				}
				
				//验证网络类型
				String nodeNetType = NodeInfoParam.NODE_NET_TYPE.trim();
				String localNoteNetType = node.getNetType();
				if(!nodeNetType.equals(localNoteNetType)){
					printStream.append("节点网络类型不一至   node.net.type["+nodeNetType+"]  已存在节点网络类型["+localNoteNetType+"] 请检查");
					printStream.println();
				}
			}
			
			//验证installIp
			String installIp = AppConst.installIp.trim();
			boolean ipflag = false;
			List<String> iplist = IpUtil.getLoclIpList();
			for (Iterator<String> iterator = iplist.iterator(); iterator.hasNext();) {
				String ip = iterator.next();
				if(installIp.equals(ip)){
					ipflag = true;
				}
			}
			if(!ipflag){
				printStream.append("非本机IP   app.install.ip["+AppConst.installIp+"] 请检查");
				printStream.println();
			}
			
			//验证tlq
			if(!nodeService.checkServerUrl(TlqConfig.TLQ_BROKER_IP.trim()+":"+TlqConfig.TLQ_JMS_PORT)){
				printStream.append("TLQ无法连接   tlq.broker.ip["+TlqConfig.TLQ_BROKER_IP+"]   tlq.broker.jms.port["+TlqConfig.TLQ_JMS_PORT+"] 请检查！");
				printStream.println();
			}
			printStream.flush();
			printStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void destroyed(ServletContextEvent contextEvent, BootParam bootParam)
			throws Exception {
	}

}
